import React, { Component } from 'react'
import { Card, Form, Input, Button, Checkbox, message } from 'antd'
import { login } from 'api/user'
import logo from 'assets/logo.png'
import './index.scss'
export default class Login extends Component {
  onFinish = async values => {
    try {
      const res = await login(values)
      console.log(res)
      message.success('登录成功')
      localStorage.setItem('jky_token', JSON.stringify(res.data))
      // 跳转页面
      this.props.history.push('/home')
    } catch (err) {
      console.log(err)
    }
  }

  render() {
    return (
      <div className="login">
        <Card className="login-container">
          <img src={logo} className="login-logo" alt="" />
          <Form
            validateTrigger={['onBlur', 'onChange']}
            size="large"
            name="basic"
            onFinish={this.onFinish}
            initialValues={{
              mobile: '13512345432',
              code: '246810',
              agree: false
            }}
            autoComplete="off"
          >
            <Form.Item
              name="mobile"
              rules={[
                {
                  required: true,
                  message: '请输入手机号!'
                },
                {
                  pattern:
                    /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
                  message: '手机号格式不正确!'
                }
              ]}
            >
              <Input />
            </Form.Item>

            <Form.Item
              name="code"
              rules={[
                {
                  required: true,
                  message: 'Please input your password!'
                },
                {
                  pattern: /^[0-9]{6}$/,
                  message: '密码格式不正确!'
                }
              ]}
            >
              <Input.Password />
            </Form.Item>

            <Form.Item
              name="agree"
              valuePropName="checked"
              rules={[
                {
                  validator: (rule, value) =>
                    value
                      ? Promise.resolve()
                      : Promise.reject(new Error('请勾选协议'))
                }
              ]}
            >
              <Checkbox>我已阅读并同意[用户协议]和[隐私条款]</Checkbox>
            </Form.Item>

            <Form.Item>
              <Button block type="primary" htmlType="submit">
                Submit
              </Button>
            </Form.Item>
          </Form>
        </Card>
      </div>
    )
  }
}
